/*
Copyright 2008  Four Roads LLC conact@4-roads.com

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either 
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections;
using System.IO;
using System.Security;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using FormLabel = CommunityServer.ControlPanel.Controls.FormLabel;
using ResourceManager = FourRoads.Links.Controls.ResourceManager;
using HelpIcon = CommunityServer.ControlPanel.Controls.HelpIcon;

namespace FourRoads.Links.Controls
{
	/// <summary>
	/// Summary description for SectionEditDetailsControl.
	/// </summary>
	public class SectionEditDetailsControl : BaseControl
	{

		#region Child Controls

		protected CommunityServer.Controls.Style ControlPanelStyle;
		protected YesNoRadioButtonList ynIsActive;
		protected YesNoRadioButtonList ynSuggestedLinks;
		protected YesNoRadioButtonList ynNotifications;
		protected YesNoRadioButtonList ynModeration;
		protected YesNoRadioButtonList ynComments;
		protected YesNoRadioButtonList ynRatings;
		protected YesNoRadioButtonList ynHitCounter;
		protected TextBox SectionName;
		protected TextBox AppKey;
		protected RequiredFieldValidator SectionNameValidator;
		protected RequiredFieldValidator AppKeyValidator;

		protected CommunityServer.ControlPanel.Controls.StatusMessage Status;

		#endregion

		#region Properties

		private int sectionID = -1;
		public int SectionID
		{
			get { return sectionID; }
			set { sectionID = value;}
		}

		private bool IsNew
		{
			get{return sectionID <= 0;}
		}

		#endregion

		override protected void OnInit(EventArgs e)
		{
			this.Load += new EventHandler(this.Page_Load);
			base.OnInit(e);
		}
		
		private void Page_Load(object sender, EventArgs e)
		{
			CSContext context = CSContext.Current;
			sectionID = context.GetIntFromQueryString("SectionID", -1);
			Status.Visible = false;

			if(!Page.IsPostBack)
			{
				BindSettingsData();
			}
		}

		public bool Save()
		{
			//Validate we are an Admin
			if(!ExtendedUserRoles.IsLinkAdministrator)
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			if(IsNew)
				return Create();
			else
				return Update();

		}


		private void BindSettingsData()
		{
			ResLinkSection s;

			if(IsNew)
			{
				s = new ResLinkSection();
			}
			else
			{
				s = ResLinkSection.GetSection(this.sectionID);
			}

			ynSuggestedLinks.SelectedValue = s.EnableSuggestLink;
			ynIsActive.SelectedValue = s.IsActive;
			ynNotifications.SelectedValue = s.EnableEmailNotification;
			ynModeration.SelectedValue = s.IsModerated;
			ynComments.SelectedValue = s.EnableComments;
			ynRatings.SelectedValue = s.EnableRatings;
			ynHitCounter.SelectedValue = s.DisplayHitCounter;
			AppKey.Text = s.ApplicationKey;
			SectionName.Text = s.Name;
		}


		private bool Update() 
		{
			ResLinkSection s = ResLinkSection.GetSection(this.sectionID);
			
			bool clearCacheParent = s.IsActive != ynIsActive.SelectedValue;

			s.EnableSuggestLink = ynSuggestedLinks.SelectedValue;
			s.IsActive = ynIsActive.SelectedValue;
			s.EnableEmailNotification = ynNotifications.SelectedValue;
			s.IsModerated = ynModeration.SelectedValue;
			s.EnableComments = ynComments.SelectedValue ;
			s.EnableRatings = ynRatings.SelectedValue;	
			s.DisplayHitCounter = ynHitCounter.SelectedValue;

			if (s.Name != SectionName.Text)
			{
				s.Name = SectionName.Text;
				if (!s.IsUniqueInCurrentContext())
				{
					Status.Text = ResourceManager.GetString("CP_ResLink_SectionEdit_StatusUpdateError");
					Status.Visible = true;
				}
	
				string formattedKey = null;
				ResLinkSection.GenerateAppKey(s.ParentID ,  SectionName.Text , out formattedKey);

				if(Globals.IsNullorEmpty(formattedKey))
				{
					AppKeyValidator.IsValid = false;
					return false;
				}

				s.ApplicationKey = formattedKey;
			}

			if(Status.Visible == false)
			{
				ResLinkSection.Update(s);

				if (clearCacheParent)
					ResLinkSection.ClearSectionCache(s.ParentID);
			}

			return !Status.Visible;
		}


		private bool Create()
		{
			//Validate we are an Admin
			if(!ExtendedUserRoles.IsLinkAdministrator)
				throw new CSException(CSExceptionType.AdministrationAccessDenied);

			ResLinkSection s = new ResLinkSection();

			//Validate the application key
			if(Globals.IsNullorEmpty(AppKey.Text))
				AppKey.Text = SectionName.Text;

			string formattedKey = null;
			ResLinkSection.GenerateAppKey(s.ParentID ,  SectionName.Text , out formattedKey);

			if(Globals.IsNullorEmpty(formattedKey))
			{
				AppKeyValidator.IsValid = false;
				return false;
			}

			s.ApplicationKey = formattedKey;
	
			//Set remaining section settings
			s.EnableSuggestLink = ynSuggestedLinks.SelectedValue;
			s.IsActive = ynIsActive.SelectedValue;
			s.EnableEmailNotification = ynNotifications.SelectedValue;
			s.IsModerated = ynModeration.SelectedValue;
			s.EnableComments = ynComments.SelectedValue ;
			s.EnableRatings = ynRatings.SelectedValue;
			s.Name = SectionName.Text;
			s.DisplayHitCounter = ynHitCounter.SelectedValue;

			if (!s.IsUniqueInCurrentContext())
			{
				Status.Text = ResourceManager.GetString("CP_ResLink_SectionEdit_StatusUpdateError");
				Status.Visible = true;
			}
			else
			{
				//Create The section
				s = ResLinkSection.Add(s);

				//Update the page SectionID for saving the permissions
				this.SectionID = s.SectionID;
			}

			return !Status.Visible;
		}
	}

}
